package com.anlogic.sdk.bsp.view.ui;

import java.net.URL;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import com.anlogic.sdk.bsp.model.IPlatformProject;
import com.anlogic.sdk.bsp.utils.PlatformUtils;
import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.FileUtils;
import com.anlogic.sdk.utils.options.ChipType;

public class HPFViewer extends FormPage implements IResourceChangeListener {
	private static final String ADDR_MAP_FIILE_PREFIX = EnvUtils.getIDERootPath() + "/data/addrmap/";
	
	private FormToolkit toolkit;

	private ScrolledForm form;

	private IProject project;

	private IPlatformProject platformProject;

	public HPFViewer(FormEditor editor, String id, String title) {
		super(editor, id, title);
		IFileEditorInput fileEditorInput = (IFileEditorInput) editor.getEditorInput();
		IFile file = fileEditorInput.getFile();
		this.project = (IProject) file.getParent();
		this.platformProject = PlatformUtils.getPlatformProjectFromIProject(this.project);
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this, 1);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		this.toolkit = managedForm.getToolkit();
		this.form = managedForm.getForm();
		this.form.setText(this.project.getName() + " Hardware Platform Specification");
		this.toolkit.decorateFormHeading(this.form.getForm());
		TableWrapLayout layout = new TableWrapLayout();
		this.form.getBody().setLayout((Layout) layout);

		addAddrMapComposite(createComposite(this.form.getBody()));
	}

	private Composite createComposite(Composite parent) {
		Composite c = this.toolkit.createComposite(parent, SWT.WRAP);
		TableWrapLayout l = new TableWrapLayout();
		l.numColumns = 1;
		c.setLayout((Layout) l);
		c.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.FILL));
		return c;
	}
	
	private void addAddrMapComposite(Composite parent) {
		String addrMapFile = "";
		String chipName = this.platformProject.getChip();
		if (ChipType.isValid(chipName)) {
			addrMapFile = ADDR_MAP_FIILE_PREFIX + chipName + "/index.html";
		}
		
		if (!FileUtils.checkFileExists(addrMapFile)) {
			this.toolkit.createLabel(parent, "Can not find address map file!");
			return;
		}
		
		URL url = FileUtils.filePathToURL(addrMapFile);
		Browser browser = new Browser(parent, SWT.NONE);
		browser.setUrl(url.toString());
		
		TableWrapData td = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.FILL);
		td.maxHeight = 1080;
		browser.setLayoutData(td);
	}

	@Override
	public void resourceChanged(IResourceChangeEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		super.dispose();
	}
}
